/*
 *  SyncIT , easy synchronization of files between computers and devices.
 *  Copyright (C) 2011  Christian Masus <cmasus(at)gmail.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version. If redistributing and/or modifying
 *  this file, this notice must be included in the redistributed or
 *  modified copy.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package gui.javagui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.EventObject;

import javax.swing.AbstractCellEditor;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.table.TableCellEditor;

@SuppressWarnings("serial")
public class SyncTableFolderEditor extends AbstractCellEditor implements TableCellEditor, ActionListener {

	private SyncTableFolderRenderer syncTableFolderRenderer;
	private int columnIndex;
	private int clickCountToStart;
	private JTextField textField;
	private JButton button;
	private JFileChooser fc;

	public SyncTableFolderEditor(SyncTableFolderRenderer syncTableFolderRenderer, int columnIndex) {
		this.syncTableFolderRenderer = syncTableFolderRenderer;
		this.columnIndex = columnIndex;
		this.clickCountToStart = 2;

		textField = new JTextField();
		textField.addActionListener(this);
		button = new JButton();
		Font font = button.getFont();
		font = font.deriveFont(Font.PLAIN);
		button.setFont(font);
		button.setHorizontalAlignment(SwingConstants.LEFT);
		button.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
		button.addActionListener(this);
		fc = new JFileChooser();
		fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
	}

	public void actionPerformed(ActionEvent e) {
		if(e.getSource().equals(button)) {
			int ret = fc.showDialog(JavaGui.getFrame(), "Select Directory");
			if(ret == JFileChooser.APPROVE_OPTION) {
				File file = fc.getSelectedFile();
				textField.setText(file.getAbsolutePath());
				fireEditingStopped();
			}
			else { // No need to edit cell if nothing was selected in the JFileChooser
				fireEditingCanceled();
			}
		}
		else { // if(e.getSource().equals(textField))
			fireEditingStopped();
		}
	}

	public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
		textField.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		textField.setText((String)value);
		button.setText((String)value);

		if(isOnBrowserButton(table, row, column)) {
			return button;
		}
		else {
			return textField;
		}
	}

	public Object getCellEditorValue() {
		return textField.getText();
	}

	public boolean isCellEditable(EventObject e) {
		JTable table = (JTable)e.getSource();
		// We only look at the x-coordinate and thats why the row number does not matter
		if(isOnBrowserButton(table, 0, columnIndex)) {
			return true;
		}
		else {
			if(e instanceof MouseEvent) {
				return ((MouseEvent)e).getClickCount() >= clickCountToStart;
			}
			return true;
		}
	}

	public boolean shouldSelectCell(EventObject anEvent) {
		return true;
	}

	public boolean stopCellEditing() {
		fireEditingStopped();
		return true;
	}

	public void cancelCellEditing() {
		fireEditingCanceled();
	}


	public boolean isOnBrowserButton(JTable table, int row, int column) {
		Rectangle rectangle = table.getCellRect(row, column, false);
		int rightMost = rectangle.x + rectangle.width;
		int leftMost = rightMost - syncTableFolderRenderer.getButtonWidth();
		Point mousePosition = table.getMousePosition();
		if(mousePosition == null) {
			return false;
		}
		int clickPosX = mousePosition.x;
		if(clickPosX >= leftMost && clickPosX <= rightMost) {
			return true;
		}
		else {
			return false;
		}
	}
}
